package com.ywger.web;

import com.ywger.web.aspect.ThreadExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.net.InetAddress;

/**
 * @author chenmeng
 */
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@ComponentScan(basePackages = "com.ywger")
@EnableScheduling
@Slf4j
public class ServerBoot {

	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(ServerBoot.class);
		ConfigurableApplicationContext applicationContext = app.run(args);

		//设置多线程 全局异常处理
		Thread.setDefaultUncaughtExceptionHandler(new ThreadExceptionHandler());


		Environment env = applicationContext.getEnvironment();
		String protocol = "http";
		if (env.getProperty("server.ssl.key-store") != null) {
			protocol = "https";
		}
		String hostAddress = "localhost";
		try {
			hostAddress = InetAddress.getLocalHost().getHostAddress();
		} catch (Exception e) {
			log.warn("The host name could not be determined, using `localhost` as fallback");
		}
		log.info("\n----------------------------------------------------------\n\t" +
						"应用 '{}' 启动成功! 访问URLS:\n\t" +
						"本地路径: \t\t{}://localhost:{}\n\t" +
						"对外路径: \t{}://{}:{}\n\t" +
						"配置:    \t{}\n----------------------------------------------------------",
				env.getProperty("spring.application.name") == null ? "UNNAMED" : env.getProperty("spring.application.name"),
				protocol,
				env.getProperty("server.port") == null ? "8080" : env.getProperty("server.port"),
				protocol,
				hostAddress,
				env.getProperty("server.port") == null ? "8080" : env.getProperty("server.port"),
				env.getActiveProfiles().length == 0 ? "[default]" : env.getActiveProfiles());


	}
}
